<?php

namespace App\Repository;

use App\Models\Role as model;

class RoleRepository extends  BaseRepository
{
  public function __construct(model $model)
  {
    $this->model = $model;
    parent::__construct();
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'name',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'name' => '权限名称不能为空',
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    //名称不能重复
    if (!empty($insertData['name'])) {
      $where = array(
        'name' => $insertData['name']
      );
      $isTrue = $this->model->where($where)->first();
      if ($isTrue) {
        return $this->returnMsg(
          3,
          '该名称已存在'
        );
      }
    }
    $res = $this->model->insert($insertData);
    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    //用户姓名不能修改
    if (isset($field['name'])) {
      $whereIs = [
        'name' => $field['name']
      ];
      $isTrue = $this->model->where($whereIs)->get();
      if (!empty($isTrue) &&  count($isTrue) > 1) {
        return $this->returnMsg(
          5,
          '该权限名称已存在'
        );
      }
    }
    //先查询是否存在
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //查看  存入缓存
  public function sel($where = [], $page = 1, $limit = 10, $user = array())
  {
    //过滤查询条件
    //权限限制
    if ($user) {
      $where['liker'] = [
        'dept_str' => $user['dept_str'] . '%'
      ];
    }

    $res = $this->search($this->model, $where, $page, $limit);
    return $res;
  }
  //查看单条数据
  public function getOne($where)
  {
    return $this->model->where($where)->first();
  }
  // 查询多条
  public function getMore($where, $whereIn = array())
  {
    $this->model =  $this->model->where($where);
    if (!empty($whereIn)) {
      $this->model->whereIn($whereIn['name'], $whereIn['value']);
    }
    return $this->model->get();
  }
  //权限处理 获取某个人的所有权限
  public function getPerm($where)
  {
    if (!empty($where['role_id']) && is_array($where['role_id'])) {
      // 当 多个权限
      $rs = $this->model->where('status', 0)->whereIn('role_id', $where['role_id'])->get();
      if (!empty($rs)) {
        $role = [
          'in_ids' => [],
          'ex_ids' => [],
        ];
        foreach ($rs as $key => $v) {
          $vArr = explode(',', $v->in_ids);
          $role['in_ids'] = array_merge($role['in_ids'], $vArr);
          $nArr = explode(',', $v->ex_ids);
          $role['ex_ids'] = array_merge($role['ex_ids'], $nArr);
        }
        return $role;
      }
    }

    return [];
  }
}
